
define(function(require, exports, module) {
    "use strict";

    var result = {};

    // 获取页面参数
    result.getQueryParms = function(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }

    // 改变时间格式
    result.changeTime = function(str, format) {
        if (!str) return "";
        if (!format) {
            format = "yyyy-mm-dd"; //  hh:ii
        };
        var date;
        if (typeof str === "string") {
            date = new Date(str.replace(/-/g, "/").replace(/T/g, " ").replace(/\+.*$/g, ""));
        } else {
            date = str;
        }

        var objForReplace = {
            y: function(length) {
                length = length < 2 ? 4 : length;
                return date.getFullYear().toString().slice(4 - length, 4);
            },
            m: function() {
                var s = date.getMonth() + 1 + "";
                if (s.length < 2) {
                    s = "0" + s;
                }
                return s;
            },
            d: function() {
                var s = date.getDate() + "";
                if (s.length < 2) {
                    s = "0" + s;
                }
                return s;
            },
            h: function() {
                var s = date.getHours() + "";
                if (s.length < 2) {
                    s = "0" + s;
                }
                return s;
            },
            i: function() {
                var s = date.getMinutes() + "";
                if (s.length < 2) {
                    s = "0" + s;
                }
                return s
            },
            s: function() {
                var s = date.getSeconds() + "";
                if (s.length < 2) {
                    s = "0" + s;
                }
                return s;
            },
            w: function() {
                var s = date.getDay();
                return "周" + "日一二三四五六".charAt(s);
            },
            p: function() {
                var s = date.getDay();
                return "6012345".charAt(s);;
            }
        };
        var reg = /y+|m+|d+|h+|i+|s+|w+|p+/gi
        var rtStr = format.replace(reg, function(r) {
            var len = r.length;
            var type = r.charAt(0);
            return objForReplace[type](len);
        });

        return rtStr;
    }

    // 获取当前日期或设置
    result.curDate = function (setMonth) {
        var now = new Date(),
            year = now.getFullYear(),       // 年
            month = now.getMonth() + 1,     // 月
            day = now.getDate(),            // 日
            date;

        // 当前时间减去设置月份
        if (setMonth) {
            month = month - parseInt(setMonth);
            if (month < 0) {
                year = year - 1;
                month = month + 12;
            }
        }

        date = year + "-";

        if (month < 10) { date += "0"; }

        date += month + "-";

        if (day < 10) { date += "0";}

        date += day;

        return date;
    }

    // 登录态
    var _dev = window.localStorage.getItem('_dev');

    // 去除前后空格
    result.trim = function(str) {
        str = (typeof str === "string") ? str.replace(/(^\s*)|(\s*$)/g, "") : str
        return str
    }
    // loading
    result.loading = {
        $load: $("#J_m_loading"),
        show (mess,type) {
            mess = (mess ? mess : '加载中')
            if (this.$load.length)
                this.$load.find(".J__loadingmess").html(mess)
            else
                this.$load = $('<div id="J__loadingF" class="m_loading"/>')
                this.$load.html('<div>'+
                    '<div class="loading__img"></div>'+
                    '<div class="loading__mess"><span class="J__loadingmess">'+
                    mess +
                    '</span><span class="dotloading"></span></div></div>'
                ).appendTo('body')
            this.$load.addClass("on")
        },
        hide () {
            this.$load.removeClass("on full");
        }
    }

    module.exports = result;
});
